package com.share.common.rabbit.MqConfig;

import com.share.common.rabbit.MqConstant.MqConst;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class QueueConfig {


    @Autowired
    private ExchangeConfig exchangeConfig;


    /**
     * mysql 同步消息 redis 队列
     * @return
     */
    @Bean
    public Queue redisQueue() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", MqConst.DEAD_LETTER_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", MqConst.ROUTING_DEAD_LETTER);
        arguments.put("x-message-ttl", 60000); // 消息TTL 60秒（可选）
        return new Queue(MqConst.QUEUE_REDIS_SYNC, true , false,  false, arguments);
    }

    /**
     * 绑定队列到交换机
     * @return
     */
    @Bean
    public Binding orderBinding() {
        return BindingBuilder.bind(redisQueue())
                .to(exchangeConfig.defaultRedisExchange())
                .with(MqConst.ROUTING_REDIS_SYNC);
    }

    /**
     * 订单队列
     * @return
     */
    @Bean
    public Queue createOrderQueue() {
        return new Queue(MqConst.QUEUE_SUBMIT_ORDER, true , false,  false);
    }

    @Bean
    public Queue endOrderQueue() {
        return new Queue(MqConst.QUEUE_END_ORDER, true , false,  false);
    }


    /**
     * 绑定队列到交换机
     * @return
     */
    @Bean
    public Binding createOrderBinding() {
        return BindingBuilder.bind(createOrderQueue())
                .to(exchangeConfig.createOrderExchange())
                .with(MqConst.ROUTING_SUBMIT_ORDER);
    }

    @Bean
    public Binding endOrderBinding() {
        return BindingBuilder.bind(endOrderQueue())
                .to(exchangeConfig.createOrderExchange())
                .with(MqConst.ROUTING_END_ORDER);
    }

    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue deadLetterQueue() {
        return new Queue(MqConst.QUEUE_DEAD_LETTER, true , false,  false);
    }

    @Bean
    public Binding deadLetterBinding() {
        return BindingBuilder.bind(deadLetterQueue())
                .to(exchangeConfig.deadLetterExchange())
                .with(MqConst.ROUTING_DEAD_LETTER);
    }


}
